<?
include('./inc/master.php');

$qas = '';
$q = $_POST['q'];
$x = ereg_replace("[^a-z0-9 ]", '', strtolower($_POST['q']));

// Process the keywords.
$keywords = explode(' ', $x);
$unique_keywords = array();
foreach ($keywords as $k) {
  if (!in_array($k, $not_keywords) && !in_array($k, $unique_keywords)) {
    $unique_keywords[] = $k;
  }
}
$k = "'". implode("', '", $unique_keywords) ."'";

// Select the most recent QAs.
if ($result = $db->query("SELECT q.question, q.answer, q.aid, q.source, UNIX_TIMESTAMP(q.happened) FROM keywords AS k JOIN qa AS q ON q.id = k.qaid WHERE k.keyword IN (". $k .") GROUP BY k.qaid, q.question, q.answer, q.aid ORDER BY COUNT(*) DESC, SUM(k.frequency) DESC LIMIT 20")) {
  
  while ($row = $result->fetch_row()) {  
    // Add the QA to the list.
    $qas .= '<li><p>'. stripslashes($row[0]) .'</p><p>'. stripslashes($row[1]) .'</p><p class="qa-meta">'. stripslashes($row[3]) .'<span>- '. date('M. j, Y, g:iA', $row[4]) .'</span></p></li>';
  }
  
  $result->close();
}

$qas = $qas == '' ? '<li></li>' : $qas;

$smarty->assign('q', stripslashes($q));
$smarty->assign('qas', $qas);
$smarty->display('qa-search.tpl');
?>
